package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.tea.inventory.db.dao.QuestionsAnswersMapper;
import org.tea.inventory.db.domain.QuestionsAnswers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionsAnswersService {

    @Resource
    private QuestionsAnswersMapper questionsAnswersMapper;
    @Autowired
    private UserService userService;


    /**
     * 分页获取 提问 列表
     */
    public List<QuestionsAnswers> querySelective(QuestionsAnswers questionsAnswers, int page, int limit, String sort) {

        List<QuestionsAnswers> lstQuestionsAnswers = null;
        PageHelper.startPage(page, limit);
        lstQuestionsAnswers = questionsAnswersMapper.selectByExample(questionsAnswers);

        return lstQuestionsAnswers;
    }

    /**
     * 获取提问列表数量
     */
    public Integer countSelective(QuestionsAnswers questionsAnswers) {

        int total = questionsAnswersMapper.countByExample(questionsAnswers);
        return total;
    }

    /**
     * 获取指定id的提问详情
     */
    public QuestionsAnswers findById(Integer id) {

        QuestionsAnswers questionsAnswers = questionsAnswersMapper.selectByPrimaryKey(id);
        return questionsAnswers;
    }

    /**
     * 录入提问信息
     */
    public void add(QuestionsAnswers questionsAnswers) {
        questionsAnswersMapper.insertSelective(questionsAnswers);
    }

    /**
     * 更新提问信息
     */
    public void update(QuestionsAnswers questionsAnswers) {
    	questionsAnswers.setIsAnswer(true);
        questionsAnswersMapper.updateByPrimaryKeySelective(questionsAnswers);

        if(questionsAnswers != null && questionsAnswers.getAnswers() != null){
            //发送通知消息
            userService.sendMessage(
                    questionsAnswers.getCreator()+"",
                    "notification",
                    "您提交的<"+ questionsAnswers.getQuestions()+"> 该问题已经有答复了![答案："+questionsAnswers.getAnswers()+"]",
                    null);
        }


    }

    /**
     * 删除提问信息
     */
    public void delete(int id) {
        questionsAnswersMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询用户提出的问题列表
     * @param questionsAnswers
     * @param page
     * @param size
     * @return
     */
	public Map<String, Object> findByCreateBy(QuestionsAnswers questionsAnswers, Integer page, Integer size) {
		PageHelper.startPage(page, size);
		List<Map> list = questionsAnswersMapper.selectBy(questionsAnswers);
		PageInfo pageinfo = new PageInfo(list);
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("total", pageinfo.getTotal());
	    resMap.put("pages", pageinfo.getPages());
		resMap.put("items", list);
		return resMap;
	}

    /**
     * 保存用户提出的问题
     * @param questionsAnswers
     * @return
     */
	public Map<String, Object> submitQuestion(QuestionsAnswers questionsAnswers) {
		int state = questionsAnswersMapper.insertSelective(questionsAnswers);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		if(state == 1) {
			resultMap.put("state", "success");
			resultMap.put("massege", "提交成功");
		} else {
			resultMap.put("state", "failuren");
			resultMap.put("massege", "提交失败");
		}
		return resultMap;
	}
}
